package com.yanggeng.demo.beginnerguidedemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

import com.yanggeng.demo.beginnerguidedemo.utils.DeviceParams;
import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.utils.AnimatorUtil;
import com.yanggeng.demo.beginnerguidedemo.utils.ToastUtil;
import com.yanggeng.demo.beginnerguidedemo.widget.HomeScrollView;

/**
 * Created by yanggeng on 2017/8/30.
 */

public class FabTestActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private RecyclerView recyclerView;
    private FloatingActionButton fab;
    private boolean isInitFAB = false;
    private String[] data = new String[50];
    private LinearLayoutManager mLinearLayoutManager;
    private View fabs;
    private FloatingActionButton fabMain, fabCzl, fabKefu;
    private HomeScrollView scrollView;
    private ImageButton ibMain, ibKefu, ibCzl;

    private Context context;
    private boolean isFabOpen = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fab);
        context = this;
//        toolbar = (Toolbar) findViewById(R.id.toolbar);
        recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
//        fab = (FloatingActionButton) findViewById(R.id.fab);

        fabs = findViewById(R.id.fabs);
//        fabMain = (FloatingActionButton) findViewById(R.id.fab_main);
//        fabCzl = (FloatingActionButton) findViewById(R.id.fab_czl);
//        fabKefu = (FloatingActionButton) findViewById(R.id.fab_kefu);
        scrollView = (HomeScrollView) findViewById(R.id.scroll_view);
        ibMain = (ImageButton) findViewById(R.id.ib_main);
        ibCzl = (ImageButton) findViewById(R.id.ib_czl);
        ibKefu = (ImageButton) findViewById(R.id.ib_kefu);
//        toolbar.setNavigationIcon(R.mipmap.c_ic_navbar_return);
//        toolbar.setTitle("fab test");
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
//        setSupportActionBar(toolbar);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mLinearLayoutManager = new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(mLinearLayoutManager);
        for (int i = 0; i < 50; i ++) {
            data[i] = "第" + i + "条数据";
        }
        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new SimpleViewHolder(LayoutInflater.from(FabTestActivity.this).inflate(R.layout.layout_simple_item, parent, false));
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                ((SimpleViewHolder) holder).tvText.setText(data[position]);
//                ((SimpleViewHolder) holder).tvText.setOnTouchListener(new View.OnTouchListener() {
//                    @Override
//                    public boolean onTouch(View v, MotionEvent event) {
//                        return gestureDetector.onTouchEvent(event);
//                    }
//                });
            }

            @Override
            public int getItemCount() {
                return data.length;
            }

            class SimpleViewHolder extends RecyclerView.ViewHolder {

                Button tvText;
                public SimpleViewHolder(View itemView) {
                    super(itemView);
                    tvText = (Button) itemView.findViewById(R.id.btn_province);
                }
            }
        });

//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mLinearLayoutManager.scrollToPosition(0);
//                hideFAB();
//            }
//        });

        scrollView.setOnScrollListener(new HomeScrollView.OnScrollListener() {
            @Override
            public void onPreScroll() {
                Log.e("FAB", "scroll view pre scroll");
                AnimatorUtil.moveX(fabs, fabs.getWidth() / 2 + DeviceParams.dip2px(context, 10f), null);
                ibMain.setClickable(false);
                if (isFabOpen) {
                    closeFabMenu();
                }
            }

            @Override
            public void onStopScroll() {
                Log.e("FAB", "scroll view stop scroll");
                AnimatorUtil.moveX(fabs, 0, null);
                ibMain.setClickable(true);
            }
        });

        ibMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFabOpen) {
                    closeFabMenu();
                } else {
                    openFabMenu();
                }
            }
        });

        ibKefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeFabMenu();
                ToastUtil.centerToast(context, "跳转到在线客服");
//                Intent intent = new Intent(context, BaseWebViewActivityNew.class);
//                intent.putExtra("title", "在线客服");
//                intent.putExtra("showLeftButton", true);
//                intent.putExtra("url", ICheApplication.ARTIFICIAL_CUSTOMER_SERVICE);
//                startActivity(intent);
            }
        });

        ibCzl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeFabMenu();
                ToastUtil.centerToast(context, "跳转到车知了");
            }
        });
    }

    private void closeFabMenu() {
        AnimatorUtil.moveYAndScaleHide(ibCzl, 0, null); // ibCzl.getHeight() * 2 + DeviceParams.dip2px(context, 20f)
        AnimatorUtil.moveYAndScaleHide(ibKefu, 0, new ViewPropertyAnimatorListener() { // ibKefu.getHeight() + DeviceParams.dip2px(context, 10f)
            @Override
            public void onAnimationStart(View view) {
            }

            @Override
            public void onAnimationEnd(View view) {
                isFabOpen = false;
            }

            @Override
            public void onAnimationCancel(View view) {
            }
        });
//        AnimatorUtil.scaleHide(ibCzl, null);
//        AnimatorUtil.scaleHide(ibKefu, new ViewPropertyAnimatorListener() {
//            @Override
//            public void onAnimationStart(View view) {
//            }
//
//            @Override
//            public void onAnimationEnd(View view) {
//                isFabOpen = false;
//            }
//
//            @Override
//            public void onAnimationCancel(View view) {
//            }
//        });
    }

    private void openFabMenu() {
        AnimatorUtil.moveYAndScaleShow(ibKefu, - (ibKefu.getHeight() + ibMain.getHeight() + DeviceParams.dip2px(context, 10f)), null);
        AnimatorUtil.moveYAndScaleShow(ibCzl, -ibMain.getHeight(), new ViewPropertyAnimatorListener() {
            @Override
            public void onAnimationStart(View view) {

            }

            @Override
            public void onAnimationEnd(View view) {
                isFabOpen = true;
            }

            @Override
            public void onAnimationCancel(View view) {

            }
        });
//        AnimatorUtil.scaleShow(ibKefu, null);
//        AnimatorUtil.scaleShow(ibCzl, new ViewPropertyAnimatorListener() {
//            @Override
//            public void onAnimationStart(View view) {
//
//            }
//
//            @Override
//            public void onAnimationEnd(View view) {
//                isFabOpen = true;
//            }
//
//            @Override
//            public void onAnimationCancel(View view) {
//
//            }
//        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
//        if (!isInitFAB) {
//            isInitFAB = true;
//            hideFAB();
//        }
    }

    private void hideFAB() {
        fab.postDelayed(new Runnable() {
            @Override
            public void run() {
                AnimatorUtil.scaleHide(fab, new ViewPropertyAnimatorListener() {
                    @Override
                    public void onAnimationStart(View view) {

                    }

                    @Override
                    public void onAnimationEnd(View view) {
                        fab.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void onAnimationCancel(View view) {

                    }
                });
            }
        }, 200);
    }
}
